import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import *

import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import*


#
# # 余额充值
# firefox = openBrowser(urlAdmin)
# DBshopLoginAdmin(firefox)
# DBshopEnterMenu2(firefox,"客户管理","管理客户")
# # 搜索用户zhangsan
# firefox.find_element_by_xpath('//input[@name="user_name"]').send_keys(userName)
# # 点击搜索
# firefox.find_element_by_xpath('//button[contains(text(),"搜索")]').click()
# # 点击余额调整
# firefox.find_element_by_link_text("余额调整").click()
# time.sleep(2)
# # 输入调整金额
# firefox.find_element_by_xpath('//input[@id="money_change_num"]').send_keys("100000")
# firefox.find_element_by_id("money_pay_info").send_keys("充值")
# firefox.find_element_by_xpath('//button[text()="提交充值"]').click()
# firefox.close()

# 打开前台网站，登录测试账号
firefox = openBrowser(url)
DBshopLogin(firefox,"zhangsan","zhangsan")


# 1.在首页搜索"小米",点击搜索按钮
firefox.find_element_by_xpath('//input[@class="search-text"]').send_keys("小米")
firefox.find_element_by_xpath('//input[@class="search-btn iconfont"]').click()

# 2.选择小米mix3,加入到购物车,点击购物车按钮
# 点击图片 切换第二个页面，进入详情页
firefox.find_element_by_xpath('//img[@alt="小米Mix3 全网通版 8GB+128GB 黑色 磁动力滑盖全面屏"]').click()
switchWindow(firefox,2)

# 点击加入购物车
# firefox.find_element_by_xpath('//a[@onclick="add_cart(8, 2);"]').click()
firefox.find_element_by_xpath('//button[@id="add_cart_submit"]').click()

time.sleep(2)
# 3.点击"去结算"按钮
firefox.find_element_by_link_text("去购物车结算").click()

# 4.默认地址已经别选择,点击下一步按钮
firefox.find_element_by_link_text("去结算").click()
# 点击下一步
firefox.find_element_by_xpath('//input[@value="下一步"]').click()

# 5.支付方式选择"货到付款",点击确认订单
firefox.find_element_by_xpath('//input[@value="hdfk"]').click()
firefox.find_element_by_xpath('//input[@value="确认订单"]').click()
#使用变量orderNO存储订单编号
orderNO=firefox.find_element_by_xpath('(//font[@color="color:#ED145B"])[2]').text
rootPath=get_pro_rootpath()
writeFile(rootPath+'\public\orderID.txt',orderNO) # 将订单编号写入到文件中

firefox.close()


# 7.登录后台管理系统,地址是"http://localhost/dbshop/admin",
firefox = openBrowser(urlAdmin)
DBshopLoginAdmin(firefox)

# 8.点击销售管理订单管理，找到刚才前台提交的订单，点击“编辑查看”。
DBshopEnterMenu2(firefox,"销售管理","订单管理")
# 输入订单号，搜索订单
firefox.find_element_by_xpath('//input[@name="order_sn"]').send_keys(orderNO)
firefox.find_element_by_xpath('//button[@class="btn btn-small btn-primary"]').click()

# 9.点击发货操作按钮，编辑快递单号“1000000000” 发货说明“保证正品”，点击“点击发货”按钮。
# 点击编辑查看
firefox.find_element_by_link_text("编辑查看").click()
firefox.find_element_by_link_text("发货操作").click()
firefox.find_element_by_id("express_number").send_keys("1000000000")
firefox.find_element_by_id("state_info").send_keys("保证正品")
# 点击发货
firefox.find_element_by_xpath('//button[contains(text(),"点击发货")]').click()
firefox.find_element_by_link_text("付款操作").click()
firefox.find_element_by_id("pay_state").click()
firefox.find_element_by_xpath('//option[text()="已付款"]').click()
firefox.find_element_by_xpath('//button[contains(text(),"提交保存")]').click()
firefox.close()

# 10.进入前台我的订单—已发货，点击“确认收货”按钮，完成订单。
firefox = openBrowser(url)
DBshopLogin(firefox,"zhangsan","zhangsan")
firefox.find_element_by_link_text("我的订单").click()
# 点击已发货
firefox.find_element_by_xpath('//a[contains(text(),"已发货")]').click()
# 搜索订单号，点击搜索
firefox.find_element_by_name('search_order_sn').send_keys(orderNO)
firefox.find_element_by_xpath('//button').click()
# 点击确认收货
firefox.find_element_by_xpath('//button[text()="确认收货"]').click()
firefox.find_element_by_xpath('//button[contains(text(),"确认收货")]').click()
firefox.switch_to.alert.accept()  #点击确认

# 11.到dbshop前台查看我的订单"交易完成"中,检查上面的订单状态

# 搜索订单号，点击搜索
firefox.find_element_by_name('search_order_sn').send_keys(orderNO)
firefox.find_element_by_xpath('//button').click()

# try:
#     firefox.find_element_by_link_text(orderNO).is_displayed()  #is_displayed()函数判断元素是否显示
#     writeLog("测试2通过")
# except:
#     writeLog("测试2失败")
#     getPhoto(firefox)
# firefox.quit()

try:
    firefox.find_element_by_link_text(orderNO).is_displayed()
    writeLog("%s，用例2成功"%orderNO)
except:
    writeLog("用例2失败")

